package android_fields;

import static android_board.GUIBoard.FIELDHEIGHT;
import static android_board.GUIBoard.FIELDWIDTH;

import common.GUIColor;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android_board.GUIBoard;
import android_board.GUICenter;
import dk.dtu.compute.course02312.matadordel2.MainActivity;
import dk.dtu.compute.course02312.matadordel2.R;

public final class GUIShipping extends GUIOwnable {
	private static final float IMAGEHEIGHTPART = 0.49f;
	private static final float TITLEHEIGHTPART = 0.24f;
//	private static final float SUBTEXTHEIGHTPART = 0.24f;
	private ImageView iconView;
	
	public static class Builder extends AndroidField.Builder<GUIShipping.Builder> implements iBuilder{
		private int pictureId = R.drawable.field_ferry1;
        public Builder() {
            this.title = "Shipping";
            this.bgColor = GUIColor.WHITE;
        }
        
        @Override
        @SuppressWarnings("synthetic-access")
        public GUIShipping build() {
            return new GUIShipping(this.pictureId, this.title,
                this.subText, this.description, this.rent, this.bgColor, this.fgColor);
        }
        
        public Builder setPictureId(int pictureId) {
            this.pictureId = pictureId;
            return this;
        }
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        public Builder setSubText(String subText) {
            this.subText = subText;
            return this;
        }
        public Builder setDescription(String description) {
            this.description = description;
            return this;
        }
        public Builder setRent(String rent) {
            this.rent = rent;
            return this;
        }
    }
	
	private GUIShipping(int pictureId, String title, String subText,
		String description, String rent, GUIColor bgColor, GUIColor fgColor) {
		super(bgColor, fgColor, title,
			subText, description, rent);
		
		Bitmap image = BitmapFactory
				.decodeResource(MainActivity.context.getResources(),
						pictureId);
		int height = (int) (IMAGEHEIGHTPART * FIELDHEIGHT);
		RelativeLayout.LayoutParams houseParams = new RelativeLayout.LayoutParams(
				FIELDWIDTH, height);
		this.iconView = new ImageView(MainActivity.context);
		this.iconView.setId(GUIBoard.generateViewId());
		this.iconView.setImageBitmap(image);
		this.iconView.setPadding(2, 2, 2, 0);
		this.iconView.setVisibility(View.VISIBLE);

		this.titleView.setText(this.title);
		this.titleView.setGravity(Gravity.CENTER);
		height = (int) (TITLEHEIGHTPART * FIELDHEIGHT);
		RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(
				FIELDWIDTH, height);

		this.subTextView.setText(this.subText);
//		height = (int) (SUBTEXTHEIGHTPART * FIELDHEIGHT);
//		RelativeLayout.LayoutParams subParams = new RelativeLayout.LayoutParams(
//				FIELDWIDTH, height);
//		subParams.addRule(RelativeLayout.ALIGN_BOTTOM, this.cell.findViewWithTag("cellsize").getId());

		this.cell.addView(this.titleView, titleParams);
		this.cell.addView(this.iconView, houseParams);
//		this.cell.addView(this.subTextView, subParams);
	}
	@Override
	public void displayOnCenter() {
		super.displayOnCenter();
		GUICenter.textViews[1].setCompoundDrawables(null, null, null, this.iconView.getDrawable());
		GUICenter.textViews[1].setVisibility(View.VISIBLE);
		GUICenter.textViews[2].setText(this.description);
		GUICenter.textViews[2].setVisibility(View.VISIBLE);
		GUICenter.textViews[3].setText(this.subText);
		GUICenter.textViews[3].setVisibility(View.VISIBLE);
		if(this.owner != null) {
			GUICenter.textViews[4].setText(this.prefix + this.owner.getName() + this.postfix);
			GUICenter.textViews[4].setVisibility(View.VISIBLE);
			GUICenter.textViews[5].setText(getLeje());
			GUICenter.textViews[5].setVisibility(View.VISIBLE);
		}
		super.displayCarOnCenter();
	}
}
